package com.test.dao.impl;

import com.test.dao.HistoryOrderDao;
import com.test.domain.HistoryOrder;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Repository
public class HistoryOrderDaoImpl implements HistoryOrderDao {

    @Resource
    private JdbcTemplate jdbcTemplate;


    @Override
    public List<String> getHistoryOrderNumListByPhoneNumberC(String customerPhoneNumber) {
        String sql="select distinct history_order_number  from history_order where customer_phone_number=?";
        return jdbcTemplate.queryForList(sql,String.class,customerPhoneNumber);
    }

    @Override
    public List<String> getHistoryOrderNumListByPhoneNumberM(String merchantPhoneNumber) {
        String sql="select distinct history_order_number  from history_order where merchant_phone_number=?";
        return jdbcTemplate.queryForList(sql,String.class,merchantPhoneNumber);
    }

    @Override
    public HistoryOrder getHistoryOrderByHistoryOrderNumber(String historyOrderNumber) {
        String sql="select * from history_order where history_order_number=?";
        return jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<>(HistoryOrder.class),historyOrderNumber);
    }

    @Override
    public int updateGradeByHistoryNumber(String historyOrderNumber, Integer merchantOrderGrade) {
        String sql="select merchant_order_grade from history_order where history_order_number=?";
        List<String> list = jdbcTemplate.queryForList(sql, String.class, historyOrderNumber);
        System.out.println(list);
        String temp=list.get(0);
        if(temp==null){
            String sql_temp="update history_order set merchant_order_grade =? where history_order_number=?";
            jdbcTemplate.update(sql_temp,merchantOrderGrade,historyOrderNumber);
            return 1;
        }else return 0;
    }

}


